﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Mvc;

    internal static partial class EWHelpers
    {
        public static partial class Web
        {
            public static partial class View
            {
                public static string GetFullViewName(string viewName = null)
                {
                    if (string.IsNullOrEmpty(viewName))
                    {
                        // get current action name
                        viewName = HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("action");
                    }

                    var extensions = new List<string>();
                    var hasExtension = false;

                    foreach (var engine in ViewEngines.Engines)
                    {
                        var viewEngine = engine as VirtualPathProviderViewEngine;
                        if (viewEngine == null) continue;

                        foreach (var extension in viewEngine.FileExtensions)
                        {
                            if (viewName.EndsWith(extension, StringComparison.InvariantCultureIgnoreCase))
                            {
                                hasExtension = true;
                            }

                            if (!extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                            {
                                extensions.Add(extension);
                            }
                        }
                    }

                    var result = default(string);

                    var controllerName = HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("controller");
                    if (hasExtension)
                    {
                        result = string.Concat("~/Areas/EasyWeb/Views/", controllerName, "/", viewName);
                    }
                    else
                    {
                        foreach (var extension in extensions)
                        {
                            var virtualPath = string.Concat("~/Areas/EasyWeb/Views/", controllerName, "/", viewName, extension);
                            var physicalPath = HostingEnvironment.MapPath(virtualPath);
                            if (File.Exists(physicalPath))
                            {
                                result = virtualPath;
                                break;
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(result))
                    {
                        throw new InvalidOperationException(string.Format("The view {0} does not exist.", viewName));
                    }

                    return result;
                }
            }
        }
    }
}
